// map.js
import qqmap from '../../../utils/qqmap'
Page({
  data: {
    latitude: 40.086757,
    longitude: 116.328634,
    detail: {},
    show: false,
    markers: [
      {
        id: 1,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 24,
        height: 30
      },
      {
        id: 2,
        latitude: 40.08346500000002,
        longitude: 116.33293800000011,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40
      }
    ],
    polyline: []
  },
  onLoad ({ id }) {
    this.getDetail(id)
    this.setData({ id })
  },
  getPolyLine () {
    qqmap.direction({
      mode: 'bicycling',
      from: `${this.data.markers[1].latitude},${this.data.markers[1].longitude}`,
      to: `${this.data.markers[0].latitude},${this.data.markers[0].longitude}`,
      success: res => {
        var ret = res
        var coors = ret.result.routes[0].polyline,
          pl = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        this.setData({
          polyline: [
            {
              points: pl,
              color: '#FFFF00',
              width: 4
            }
          ]
        })
      }
    })
  },
  async getDetail (id) {
    const res = await wx.$http.get(`/repair/${id}`)
    if (res.code !== 10000) return wx.$utils.toast()
    this.setData({
      detail: res.data
    })
    if (this.data.detail.status === 2) {
      this.getPolyLine()
    }
  },
  cancelRepair () {
    this.setData({ show: true })
  },
  async confirmCancel () {
    const res = await wx.$http.put(`/cancel/repaire/${this.data.id}`)
    if (res.code !== 10000) return wx.$utils.toast()
    wx.$utils.toast('取消成功')
    wx.navigateBack({ delta: 1 })
  },
  goEdit () {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.id
    })
  }
})
